import {request } from 'ice';
import {API_URL} from './ApiUrl'
import {Message} from '@alifd/next';
/**
 * 上传附件接口
 */
async function upLoadImage(fileName,file) {
    try {
        const formData = new FormData();
        formData.append(fileName, file);
        const response = await request.post(API_URL.API_COMMON_UPLOAD_IMAGE, formData, {
          headers: {'Content-Type': 'multipart/form-data;'},
        });
        if(response.status == 0){
            return response.data;
        }else if(response.status == 41501){
          Message.error(response.msg)
        }else{
            const msg = response.msg ? response.msg : "服务忙, 请稍后再试!"
            Message.error(msg)
        }
    } catch (error) {
      console.error(error);
    }
  }
/**
 * 
 * @param {*} file 
 * return:
 *   {
    "status": 0,
    "data": {
        "fileId": 2,
        "appName": "学习商城",
        "pkgName": "com.flutter_shop_study",
        "versionCode": 1,
        "versionName": "1.0.0",
        "downLoadUrl": "http://127.0.0.1/tiku/download/niandaxue_v1.0.0.apk",
        "appMd5": "ee5f05ce58f89eaea982534565f76bb0",
        "appSize": 17894751
    }
  }
 */
async function upLoadApk(file) {
  try {
      const formData = new FormData();
      formData.append('file', file);
      const response = await request.post(API_URL.API_VERSION_UPLOAD, formData, {
        headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'},
      });
      if(response.status == 0){
          return response.data;
      }else{
          const msg = response.msg ? response.msg : "服务忙, 请稍后再试!"
          Message.error(msg)
      }
  } catch (error) {
    console.error(error);
  }
}

export {upLoadImage, upLoadApk}